Skip to content

Feature: spatially explicit detectability covariates through a new cov.surface interface#99

Open
hambrecht wants to merge 3 commits into
DistanceDevelopment:masterfrom
hambrecht:feature/raster-covariate-upstream-clean
Open

Feature: spatially explicit detectability covariates through a new cov.surface interface#99
hambrecht wants to merge 3 commits into
DistanceDevelopment:masterfrom
hambrecht:feature/raster-covariate-upstream-clean

Conversation

@hambrecht
Copy link
Copy Markdown

@hambrecht hambrecht commented May 5, 2026

Summary
This PR adds support for spatially explicit detectability covariates through a new cov.surface interface.
Users can now provide canopy (or other environmental) rasters and combine them with existing individual covariates in a single detectability model.

What Changed

  1. Added terra dependency in package metadata.
  2. Extended Detectability to include cov.surface.
  3. Updated make.detectability to accept cov.surface.
  4. Updated scale parameter calculation to extract raster values per individual location.
  5. Added simulation-level validation for cov.surface and cov.param consistency.
  6. Fixed serial simulation execution path and warning display behavior used by integration tests.
  7. Updated internal test call to use namespaced internal helper.
  8. Added a full raster covariate test suite.
  9. Fixed plotting for mixed covariates so raster covariates are treated as valid during plot(detectability, population.description).

Behavior
Detection scale now supports mixed effects of individual and spatial covariates using:

log(sigma_i) = log(sigma_0) + sum(beta_k * z_ik)

where z_ik may come from:

  1. Individual covariates in population description.
  2. Raster cell values from cov.surface at each individual location.

Validation and Errors

  1. cov.surface entries must be named.
  2. cov.surface names must exist in cov.param.
  3. cov.surface values must be a SpatRaster or a file path.
  4. Missing raster files are rejected at validation time.
  5. Raster NA values at sampled locations are handled with warning and fallback behavior.

Tests

  1. Added dedicated raster covariate tests covering:
  2. Directional effect of canopy on detectability.
  3. File-path versus in-memory raster parity.
  4. Validation failures for malformed cov.surface.
  5. No-regression behavior when cov.surface is not used.
  6. run.simulation integration with raster covariate.
  7. Mixed size + canopy plotting path.

Notes
This PR is scoped to raster covariate detectability support and related fixes needed for end-to-end simulation and plotting workflows.
Related to Issue 96

hambrecht added 2 commits May 5, 2026 15:37
- add cov.surface support to Detectability and make.detectability
- apply raster covariates in calculate.scale.param
- validate cov.surface in simulation checks
- add raster covariate test coverage
- update related simulation/covariate paths used by tests
Update Detectability plot validation so covariates supplied via cov.surface are treated as valid even when absent from population covariates. Add raster-specific plotting path that reads surface values and uses raster quantiles to generate representative detection curves, enabling mixed covariate plotting (for example size + canopy) without false missing-covariate errors.
Copilot AI review requested due to automatic review settings May 5, 2026 22:44
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds spatially explicit detectability covariates via a new cov.surface interface, allowing detectability scale to depend on raster values sampled at each individual’s simulated location (optionally combined with existing individual-level covariates).

Changes:

  • Added cov.surface support to Detectability / make.detectability, including validation and plotting support for mixed covariates.
  • Updated calculate.scale.param() to sample raster covariates at animal locations and incorporate them into the log-linear scale calculation.
  • Added a dedicated raster covariate test suite and adjusted an existing test to use the internal namespaced helper.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
DESCRIPTION Adds terra as an imported dependency to support raster covariates.
R/Detectability.R Extends Detectability with cov.surface, adds validation, and updates plotting to handle raster covariates.
R/ClassConstructors.R Extends make.detectability() API and documents the new cov.surface parameter.
R/calculate.scale.param.R Extracts raster values at individual locations and uses them as covariates in scale parameter computation.
R/check.simulation.R Adds simulation-level consistency check for cov.surface names vs cov.param.
tests/testthat/test-check_RasterCovariate.R Adds comprehensive tests for raster covariate behavior and validation.
tests/testthat/test-check_IndividualCovariates.R Updates test to call dsims:::calculate.scale.param() explicitly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread R/calculate.scale.param.R
Comment on lines +49 to +55
if(any(is.na(vals))){
warning(paste0("NA raster values for surface covariate '", sn,
"' at some animal locations (outside raster extent). ",
"Replacing with global raster mean."),
call. = FALSE, immediate. = TRUE)
vals[is.na(vals)] <- mean(vals, na.rm = TRUE)
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment thread R/calculate.scale.param.R
Comment on lines +46 to +48
cell_ids <- terra::cellFromXY(surf, pts)
rast_vals <- terra::values(surf)[, 1]
vals <- rast_vals[cell_ids]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment thread R/Detectability.R
Comment on lines +146 to +147
}
}
Comment thread R/Detectability.R
Comment on lines +231 to +240
if(is.character(cov.surface)){
cov.surface <- terra::rast(cov.surface)
}
surf.values <- terra::values(cov.surface)[,1]
surf.values <- surf.values[!is.na(surf.values)]
if(length(surf.values) == 0){
quantiles <- c(0, 0, 0)
}else{
quantiles <- as.numeric(quantile(surf.values, c(0.025, 0.5, 0.975)))
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Comment thread R/Detectability.R
Comment thread R/ClassConstructors.R
#' Use a negative \code{cov.param} slope to model lower detectability where raster values
#' are higher (e.g. denser canopy cover reduces detection range).
#' Animal locations outside the raster extent or with NA cell values are replaced with the
#' raster mean and a warning is issued.
Comment thread R/Detectability.R Outdated
Updated the @slot cov.param docs to reflect the actual allowed shapes (numeric vectors for continuous/discrete + data.frames for categorical levels).

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@hambrecht
Copy link
Copy Markdown
Author

@copilot apply changes based on the comments in this thread

@hambrecht hambrecht requested a review from Copilot May 6, 2026 15:40
@hambrecht hambrecht changed the title Feature/raster covariate upstream clean Feature: spatially explicit detectability covariates through a new cov.surface interface May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants